import { createRouter, createWebHashHistory } from 'vue-router'
import { useGlobeStore, useUserStore } from '@/stores'
import routes from './routes'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
})

// 全局前置守卫
router.beforeEach(async(to, from) => {
  useGlobeStore().showLoading();
  // url上携带mt 就获取用户信息
  const { mt } = to.query;
  if (mt) await useUserStore().getUserInfo(mt);
  if (!to.query.mt && from.query.mt){
    return {
      ...to,
      query: {
        ...to.query,
        mt: from.query.mt, // 传递参数
        token: from.query.token // 传递参数
      }
    }
  }else{
    return true
  }
})

// 全局后置守卫
router.afterEach(() => {
  useGlobeStore().hideLoading();
})

export default router
